<?php

/**
 * 自动生成 WxEventAwardController
 * 
 * Apps/Mp/Controller/WxEventAwardController.class.php
 * cmcc_admin @ 2015-07-06
 */

namespace Mp\Controller;

use Common\Service\WxEventAwardService;

class WxEventAwardController extends MpController {

  public function index( $event_type = '' , $event_id = 0  ) {
    $action = I('get.action', 'read');

    $event_obj_name = 'Common\Service\WxEvent'.$event_type.'Service';
    $EventObj = $event_obj_name::instance();
    $event_data = $EventObj->get_by_id($event_id);
    
    if ( $event_data['mer_id'] != $this->mer_id ) {
      gameover('商户信息错误');
    }
    
    $data = $this->_init_data();
    $data['module_name'] = '活动奖品';
    $data['module_url'] = U( $this->base_path . 'index');

    $data['event_name'] = $EventObj->name . ' 活动';
    $data['event_url'] = U('Mp/WxEvent'. $event_type .'/index') . '/' . $event_data['id'];
    
    $WxEventAward = WxEventAwardService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => 'menu_Mp_MerWxSetting_index',
      'event_type' => $event_type ,
      'event_id'   => $event_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'award_sn_url' => U('Mp/WxEventAwardSn/index' ) . '/',
      'row' => $WxEventAward->get_default_row() 
    );

    $data['param'] = array(
      'rank' => $WxEventAward->rank ,
      'valid_type' => $WxEventAward->valid_type ,
    );

    $this->display_with_layout($data, $this->controller .':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'event_type' => $post['event_type'],
      'event_id'   => $post['event_id'] ,
    );

    $WxEventAward = WxEventAwardService::instance();
    $data['rows'] = $WxEventAward->get_by_cond($config);

    $data['total'] = 1;

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $post['event_type'] = $post['event_type'];
    $post['event_id'] = $post['event_id'];
    $post['remain_qty'] = $post['total_qty'];
    
    if( $post['valid_type'] != 1 ) {
      unset( $post['password'] );
    } else {
      if ( empty( $post['password'] ) ) {
        $this->ajaxReturn(ajax_arr('请设置兑换密码'));
      }
    }
    
    $WxEventAward = WxEventAwardService::instance();
    $ret = $WxEventAward->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    if( $post['valid_type'] != 1 ) {
      unset( $post['password'] );
    } else {
      if ( empty( $post['password'] ) ) {
        $this->ajaxReturn(ajax_arr('请设置兑换密码'));
      }
    }
    
    $WxEventAward = WxEventAwardService::instance();
    $ret = $WxEventAward->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $WxEventAward = WxEventAwardService::instance();
    $ret = $WxEventAward->delete($ids); 
    $this->ajaxReturn($ret);
  }
}
